import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store/index'

Vue.use(VueRouter)
//是否为手机端
const isMobile = store.state.isMobile

const routes = [{
    path: '/',
    redirect: isMobile ? '/mobile' : '/pc',
},
{
    path: '/pc',
    name: 'pc',
    component: () => import( /* webpackChunkName: "pc" */ '../views/pc.vue')
}, {
    path: '/mobile',
    name: 'mobile',
    component: () => import( /* webpackChunkName: "mobile" */ '../views/mobile.vue')
}
]

const router = new VueRouter({
    mode: 'hash',
    routes
})

//路由守卫 跳转前
router.beforeEach((to, from, next) => {
    //移动端跳转/pc 重定向为/mobile
    if (to.name === 'pc' && isMobile) next('/mobile')
    //pc端跳转/mobile 重定向为/pc
    if (to.name === 'mobile' && !isMobile) next('/pc')
    //正常情况
    if (to.name === 'pc' && !isMobile || to.name === 'mobile' && isMobile) next()
})

export default router